package com.polaris.live.ui.user

import android.os.Bundle
import android.text.Editable
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.KeyboardUtils
import com.polaris.live.R
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.ActivitySearchUserBinding
import com.polaris.live.interface_bag.TextWatcherAdapter
import com.polaris.live.translation.R.string.please_input_context
import com.polaris.live.viewmodel.SearchUserViewModel

/**
 * 公会用户搜索页
 *
 * @author Created by 半仙 on 2024/9/10/010 15:38
 */
class SearchUserActivity : BaseVbActivity<SearchUserViewModel, ActivitySearchUserBinding>() {

    private val mFragmentList = mutableListOf<Fragment>()

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.consTitle)
        setStatusBarDarkFont(true)
        setNavigationBarDarkFont(true)
        attachImmersionBar { transparentBar() }
        ViewUtils.setMargin(
            mBinding.vpList,
            bottomMargin = NavigationBarUtils.getNavigationBarHeight(this@SearchUserActivity)
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        initListener()
        initObserver()
        KeyboardUtils.showSoftInput(mBinding.etSearch)
        mFragmentList.add(SearchUserFragment().apply {
            arguments = bundleOf("type" to USER)
        })
        mFragmentList.add(SearchUserFragment().apply {
            arguments = bundleOf("type" to ROOM)
        })
        mBinding.vpList.adapter =
            ViewPagerAdapter(mFragmentList, this)
        mBinding.vpList.offscreenPageLimit = 2
    }

    private fun initObserver() {
        mViewModel.searchRoomLiveData.observe(this) { resultState ->
            val fragment =
                mFragmentList.getOrNull(ROOM) as? SearchUserFragment ?: return@observe
            parseState(resultState, {
                fragment.onSearchRoomCallback(it)
            }, {
                if (mBinding.vpList.currentItem == ROOM) {
                    ToastGravity.showGravity(it.errorMsg)
                }
                fragment.onSearchRoomCallback(emptyList())
            })
        }
        mViewModel.searchOtherLiveData.observe(this) { resultState ->
            val fragment =
                mFragmentList.getOrNull(USER) as? SearchUserFragment ?: return@observe
            parseState(resultState, {
                fragment.onSearchUserCallback(it)
            }, {
                if (mBinding.vpList.currentItem == USER) {
                    ToastGravity.showGravity(it.errorMsg)
                }
                fragment.onSearchUserCallback(emptyList())
            })
        }

    }

    private fun initListener() {
        mBinding.tvUser.setOnSingleClickListener {
            changePage(USER)
            mBinding.vpList.currentItem = USER
        }
        mBinding.tvRoom.setOnSingleClickListener {
            changePage(ROOM)
            mBinding.vpList.currentItem = ROOM
        }
        mBinding.vpList.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                changePage(position)
            }
        })
        mBinding.ivBack.setOnSingleClickListener {
            finish()
        }
        mBinding.etSearch.addTextChangedListener(object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                val string = mBinding.etSearch.text.toString()
                if (string.isNotEmpty()) {
                    mBinding.tvSearch.setBackgroundResource(R.drawable.search_buttom_back)
                    mBinding.ivClear.visibility = View.VISIBLE
                } else {
                    mBinding.tvSearch.setBackgroundResource(R.drawable.search_bottom_dont_back)
                    mBinding.ivClear.visibility = View.GONE
                }
            }
        })
        mBinding.ivClear.setOnSingleClickListener {
            mBinding.ivClear.visibility = View.GONE
            mBinding.etSearch.setText("")
        }
        mBinding.tvSearch.setOnSingleClickListener {
            val string = mBinding.etSearch.text.toString()
            val userNumber = string.toLongOrNull() ?: return@setOnSingleClickListener
            if (string.length > 0) {
                mViewModel.searchOtherUser(userNumber)
                mViewModel.searchRoom(userNumber)
            } else {
                ToastGravity.showGravity(please_input_context)
            }
        }
    }

    private fun changePage(position: Int) {
        if (position == 0) {
            mBinding.tvUser.isSelected = true
            mBinding.tvRoom.isSelected = false
        } else {
            mBinding.tvUser.isSelected = false
            mBinding.tvRoom.isSelected = true
        }
    }

    companion object {

        const val USER = 0          //用户
        const val ROOM = 1          //房间
    }
}